package com.dxg.config;

import cn.hutool.core.collection.CollectionUtil;
import com.dxg.provider.SmsAuthenticationProvider;
import com.dxg.provider.service.SmsUserDetailService;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author dingxigui
 * @version 1.0
 * @date 2021/7/30 10:53
 */
@Configuration
public class CommonBeanConfig {

  @Bean
  public PasswordEncoder passwordEncoder(){
    return NoOpPasswordEncoder.getInstance();
  }

  @Bean
  public UserDetailsService userDetailsService(){
    return username -> {
      User user = new User("dingding", "12345", CollectionUtil.newArrayList(new SimpleGrantedAuthority("q1")));
      return user;
    };
  }


  /**
   * 由于覆盖了
   * com.dxg.config.CustomerSecurityConfig#configure(org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder)
   * 所以下面2个成员变量需要设置
   * @param userDetailsService
   * @param passwordEncoder
   * @return
   */
  @Bean
  @Resource
  public AuthenticationProvider daoAuthenticationProvider (UserDetailsService userDetailsService,PasswordEncoder passwordEncoder){
    DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
    daoAuthenticationProvider.setUserDetailsService(userDetailsService);
    daoAuthenticationProvider.setPasswordEncoder(passwordEncoder);
    return daoAuthenticationProvider;
  }


}
